package com.quanquan.store.service;

import com.quanquan.store.dto.AdsenceDTO;
import com.quanquan.store.entity.Adsence;
import com.quanquan.store.repository.AdsenceRepository;
import com.quanquan.store.request.AdsenceRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.UUIDGenerator;

import javax.persistence.criteria.Predicate;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName:BannerTypeService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/3/14 10:40
 * @Version 1.0
 */
@Service
public class AdsenceService {

    @Autowired
    private AdsenceRepository adsenseRepository;

    /**
     * 新增广告位
     *
     * @param request
     * @return
     */
    public boolean saveAdsence(AdsenceRequest request) {

        Adsence adsense = new Adsence();
        Date date = new Date();
        adsense.setId(UUIDGenerator.getUUID());
        adsense.setCreateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        adsense.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        adsense.setStatusShow(request.getStatusShow());
        adsense.setStatusRemove(0);
        adsense.setBannerType(request.getBannerType());
        adsense.setName(request.getName());
        adsense.setPattern(request.getPattern());
        adsense.setStandard(request.getStandard());
        adsense.setNumber(request.getNumber());
        adsense.setStatus(request.getStatus());
        adsense.setStatusUrl(request.getStatusUrl());
        adsense.setShowType(request.getShowType());
        adsense.setShowTime(request.getShowTime());
        adsenseRepository.saveAndFlush(adsense);
        return true;

    }


    /**
     * 修改广告位信息
     *
     * @param request
     * @return
     */
    public boolean updateAdsense(AdsenceRequest request) {
        Date date = new Date();
        Adsence adsense = adsenseRepository.findByAdsenceId(request.getId());
        if (adsense == null) {
            return false;
        }
        adsense.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        adsense.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        adsense.setStatusShow(request.getStatusShow());
        adsense.setStatusRemove(0);
        adsense.setBannerType(request.getBannerType());
        adsense.setName(request.getName());
        adsense.setPattern(request.getPattern());
        adsense.setStandard(request.getStandard());
        adsense.setNumber(request.getNumber());
        adsense.setStatus(request.getStatus());
        adsense.setStatusUrl(request.getStatusUrl());
        adsense.setShowType(request.getShowType());
        adsense.setShowTime(request.getShowTime());
        adsenseRepository.saveAndFlush(adsense);
        AdsenceDTO adsenceDTO = new AdsenceDTO();
        BeanUtils.copyProperties(adsense, adsenceDTO);
        return true;
    }


    /**
     * 删除广告位（软删除）
     *
     * @param id
     * @return
     */
    public boolean removeAdsense(String id) {
        Date date = new Date();
        Adsence adsense = adsenseRepository.findByAdsenceId(id);
        adsense.setStatusRemove(1);
        adsense.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        adsenseRepository.saveAndFlush(adsense);
        return true;
    }

    /**
     * 广告位详情
     *
     * @param id
     * @return
     */
    public AdsenceDTO query(String id) {
        Date date = new Date();
        Adsence adsense = adsenseRepository.findByAdsenceId(id);
        AdsenceDTO adsenceDTO = new AdsenceDTO();
        BeanUtils.copyProperties(adsense, adsenceDTO);
        return adsenceDTO;
    }

    /**
     * 查询广告位列表
     *
     * @param request
     * @return
     */
    public List<AdsenceDTO> queryList(AdsenceRequest request) {
        List<AdsenceDTO> adsenceDTOList = new LinkedList<>();
        List<Adsence> adsenseList = null;
        //分页
        Sort sort = new Sort(Sort.Direction.ASC, "createTime");
        Integer page = request.getPage() == null ? 0 : request.getPage();
        Integer size = request.getSize() == null ? 100 : request.getSize();
        Pageable pageable = PageRequest.of(page, size, sort);
        Adsence adsense = new Adsence();
        adsense.setStatusRemove(0);
        Example<Adsence> example = Example.of(adsense);
        Page<Adsence> findAll = adsenseRepository.findAll(example, pageable);
        adsenseList = findAll.getContent();
        for (Adsence localadsence : adsenseList) {
            AdsenceDTO adsenceDTO = new AdsenceDTO();
            BeanUtils.copyProperties(localadsence, adsenceDTO);
            adsenceDTOList.add(adsenceDTO);
        }
        return adsenceDTOList;
    }

    /**
     * @param bannerType
     * @return
     */
    public List<Adsence> queryByBannerType(Integer bannerType, Integer page, Integer size) {
        Integer count = page * size;
        List<Adsence> adsenseList = adsenseRepository.findByBannerType(bannerType, 0, count, size);
        return adsenseList;
    }

    public List<Adsence> findListByKeyword(String keyword) {
        List<Adsence> adsenceList = adsenseRepository.findAll((Specification<Adsence>) (root, query, criteriaBuilder) -> {
            Predicate p = criteriaBuilder.like(root.get("name").as(String.class), "%" + keyword + "%");
            return query.where(p).getRestriction();
        });
        return adsenceList;
    }

}
